package trabalhotec.gui;

import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.awt.*;
import trabalhotec.BD.TelaLogin;

public class LoginTela extends JInternalFrame {

    JTextField nomeuser;
    JPasswordField password;
    TelaLogin ver = new TelaLogin();

    public LoginTela() {
        JPanel pane = new JPanel();
        this.setTitle("Tela Login");
        FlowLayout flow = new FlowLayout();
        BorderLayout border = new BorderLayout();
        border.setVgap(5);
        flow.setAlignment(FlowLayout.TRAILING);
        pane.setLayout(border);

        JPanel top = new JPanel();
        JPanel bot = new JPanel();
        JPanel botoes = new JPanel();
        JLabel nomeus = new JLabel("Nome do usuario");
        JLabel pass = new JLabel("Password");
        JButton ok = new JButton("Ok");
        JButton cancel = new JButton("Cancel");
        nomeuser = new JTextField("", 10);
        password = new JPasswordField("", 10);

        top.setLayout(flow);
        bot.setLayout(flow);
        botoes.setLayout(flow);

        botoes.add(ok);
        botoes.add(cancel);

        top.add(nomeus);
        top.add(nomeuser);
        bot.add(pass);
        bot.add(password);

        pane.add(top, BorderLayout.NORTH);
        pane.add(bot, BorderLayout.CENTER);
        pane.add(botoes, BorderLayout.SOUTH);

        this.add(pane);

        ok.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                String login = nomeuser.getText();
                String senha = new String(password.getPassword());
                int q = ver.verificarLogin(login, senha);
                setVisible(false);
                if (q != -1) {
                    Aplicacao.aut = q;
                    fireInternalFrameEvent(InternalFrameEvent.INTERNAL_FRAME_CLOSED);
                }
                else{
                    JOptionPane.showMessageDialog(null,"login e/ou senha invalida");
                    setVisible(true);
                }

            }
        });
        cancel.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

    }
}
